package liveness.graph;

import util.List;


public class Node{
	
	/***/
    Graph mygraph;
    
    /***/
    @SuppressWarnings("unused")
	private Node(){}
    
    /***/
    public int mykey;
    
    /**
     * 
     * */
    public Node(Graph g){
        mygraph=g; 
        mykey= g.nodecount++;
        NodeList p = new NodeList(this, null);
        if (g.mylast==null)
            g.mynodes=g.mylast=p;
        else
            g.mylast = (NodeList) (g.mylast.tail = p);
    }

    /***/
    NodeList succs;
    /***/
    NodeList preds;
    
    /**
     * 
     * */
    public NodeList succ(){
        return succs;
    }
    
    /**
     * 
     * */
    public NodeList pred(){
        return preds;
    }
    
    /**
     * 
     * */
    NodeList cat(List<Node> tail, NodeList b){
        if (tail==null)
            return b;
        else return new NodeList(tail.head, cat(tail.tail,b));
    }
    
    /**
     * 
     * */
    public NodeList adj(){
        return cat(succ(), pred());
    }

    /**
     * 
     * */
    int len(NodeList l){
        int i=0;
        for(NodeList p=l; p!=null; p=(NodeList) p.tail)
            i++;
        return i;
    }

    /**
     * 
     * */
    public int inDegree(){
        return len(pred());
    }
    
    /**
     * 
     * */
    public int outDegree(){
        return len(succ());
    }
    
    /**
     * 
     * */
    public int degree(){
        return inDegree()+outDegree();
    } 

    /**
     * 
     * */
    public boolean goesTo(Node n){
        return Graph.inList(n, succ());
    }

    /**
     * 
     * */
    public boolean comesFrom(Node n){
        return Graph.inList(n, pred());
    }

    /**
     * 
     * */
    public boolean adj(Node n){
        return goesTo(n) || comesFrom(n);
    }

    /**
     * 
     * */
    public String toString(){
        return String.valueOf(mykey);
    }
}
